package us.mooresr.geartrack.client.core.desktop;

import java.util.List;

import us.mooresr.geartrack.client.core.presenter.PeoplePresenter;
import us.mooresr.geartrack.client.core.presenter.PeopleUiHandlers;
import us.mooresr.geartrack.shared.model.Person;
import us.mooresr.geartrack.shared.model.DatastoreObject;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;

public class DesktopPeopleViewImpl 
	extends ViewWithUiHandlers<PeopleUiHandlers> 
	implements PeoplePresenter.MyView, ContentWidgetEventHandler {

	private final Widget widget;
	private final ContentWidget contentWidget;
	private SingleItemForm singleItemForm;
	private Person selectedPerson;
	@UiField HTMLPanel contentPanel;
	
	public interface Binder extends UiBinder<Widget, DesktopPeopleViewImpl> {
	}

	@Inject
	public DesktopPeopleViewImpl(final Binder binder) {
		widget = binder.createAndBindUi(this);
		contentWidget = GWT.create(ContentWidget.class);
		contentWidget.setTitle("People");
		contentPanel.add(contentWidget);
		contentWidget.setHandler(this);
		
		singleItemForm = new SingleItemForm();
		contentWidget.setContent(singleItemForm);
		
		// Assume we start with no item selected and an empty form, so disable the save and delete buttons.
		clearSelection();
	}

	private void clearSelection() {
		selectedPerson = null;
		contentWidget.clearSelection();
		contentWidget.disableButton(1);
		contentWidget.disableButton(2);
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void onSelection(DatastoreObject selectedObject) {
		selectedPerson = (Person)selectedObject;
		
		singleItemForm.setName(selectedPerson.getName());

		
		contentWidget.enableButton(1);
		contentWidget.enableButton(2);
		
	}

	@Override
	public void onButton1Click() {
		// Save Button

		String name = singleItemForm.getName();
		if ((name == null) || (name.length() == 0)) {
			// Must have a name
			Window.alert("Name can not be blank.");
			return;
		}

		// End of validation
		selectedPerson.setName(singleItemForm.getName());
		

		getUiHandlers().updatePerson(selectedPerson);
		
		singleItemForm.clear();
		
	}

	@Override
	public void onButton2Click() {
		// Delete Button
		if (selectedPerson == null) {
			return;
		}
		
		String name = selectedPerson.getName();
		String message = "Are you sure you want to delete " + name + "?";
		boolean answer = Window.confirm(message);
		
		if (answer) {
			getUiHandlers().deletePerson(selectedPerson);
			singleItemForm.clear();
		}
		
	}

	@Override
	public void onButton3Click() {
		// Add Button

		String name = singleItemForm.getName();
		if ((name == null) || (name.length() == 0)) {
			// Must have a name
			Window.alert("Name can not be blank when adding a new person.");
			return;
		}

		// End of validation
		Person newBorrower = new Person();
		newBorrower.setName(name);

		getUiHandlers().addPerson(newBorrower);
		
		singleItemForm.clear();
	}

	@Override
	public void setPersonList(List<Person> persons) {
		contentWidget.setListContents(persons);
		clearSelection();
		
	}

	@Override
	public void addPersonToList(Person person) {
		contentWidget.addItemToList(person);
		
	}

	@Override
	public void removePersonFromList(Person person) {
		contentWidget.removeItemFromList(person);
		clearSelection();
	}

	@Override
	public void updateListEntry(Person person) {
		contentWidget.updateListItem(person);
		clearSelection();
	}

}
